<?php

function addMenuTemplate($name, $type)
{		
	$result = mysql_query('SELECT template_id FROM '.DB_PERFIX.'menu_templates ORDER BY template_id DESC LIMIT 1;');
	while($row = mysql_fetch_array($result))
	{ $item['template_id'] = $row['template_id']; }
	$item['template_id']++;
	
	
	$insert  = 'INSERT INTO '.DB_PERFIX.'menu_templates (`template_id`, `name`, `type`)'; 
	$insert .= 'VALUES ('.$item['template_id'].', "'.$name.'", "'.$type.'");';
	$result = mysql_query($insert);
	
	echo $insert;
	$result = mysql_query('SELECT id FROM '.DB_PERFIX.'menu_templates WHERE level=0 ORDER BY id DESC LIMIT 1;');
	while($row = mysql_fetch_array($result))
	{ $item['id'] = $row['id']; }
	return $item['id'];
}
function addMenuLevel($id)
{		
	$result = mysql_query('SELECT * FROM '.DB_PERFIX.'menu_templates WHERE template_id='.$id.' AND level=0 LIMIT 1;');
	while($row = mysql_fetch_array($result))
	{
		$menu['template_id'] = $row['template_id'];
		$menu['name'] = $row['name'];
		$menu['type'] = $row['type'];
		$menu['level'] = $row['level'];
	}
	$menu['level']++;
	
	$insert  = 'INSERT INTO '.DB_PERFIX.'menu_templates (`template_id`, `name`, `type`, `level`)'; 
	$insert .= 'VALUES ('.$id.', "'.$menu['name'].'", '.$menu['type'].', '.$menu['level'].');';
	$result = mysql_query($insert);
	echo $insert;
	return $result;
}	
function getMenuTemplate($id)
{
	$result = mysql_query('SELECT * FROM '.DB_PERFIX.'menu_templates WHERE id='.$id.' LIMIT 1;');
	while($row = mysql_fetch_array($result))
	{
		$item['max_levels'] = 0; 
		$item['id'] = $row['id'];
		$item['template_id'] = $row['template_id'];
		$item['name'] = $row['name'];
		$item['type'] = $row['type'];
		$item['level'] = $row['level'];
		$item['height'] = $row['height'];
		$item['image_l'] = $row['image_l'];
		$item['image_r'] = $row['image_r'];
		$item['image'] = $row['image'];
		$item['image_bg'] = $row['image_bg'];
		$item['image_hov'] = $row['image_hov'];
		$item['image_act'] = $row['image_act'];
		$item['style'] = $row['style'];
		$item['active'] = $row['active'];
		//Top menu std
		if(($item['type'] == 0) && ($item['level'] == 0))
		{ $item['max_levels'] = 1; }
		//Side menu std
		if(($item['type'] == 1) && ($item['level'] == 0))
		{ $item['max_levels'] = 1; }
		//Top menu fixed
		if(($item['type'] == 2) && ($item['level'] == 0))
		{ $item['max_levels'] = 30; }
	}
	
	if($item['template_id'] > 0)
	{
		$result = mysql_query('SELECT * FROM '.DB_PERFIX.'menu_templates WHERE template_id='.$item['template_id'].' AND level > '.$item['level'].';');
		while($row = mysql_fetch_array($result))
		{
			$item['child_id'] = $row['id'];
		}
	}
	return $item;
	
}
function setMenuTempType($id, $value)
{
	$update = mysql_query('UPDATE '.DB_PERFIX.'menu_templates SET type='.$value.' WHERE id='.$id.' LIMIT 1;');
	return $update;
}

function setMenuName($id, $value)
{
	$update = 'UPDATE '.DB_PERFIX.'menu_templates SET name="'.$value.'" WHERE id='.$id.' LIMIT 1;';
	$update = mysql_query($update);
	return $update;
}

function setMenuContType($id, $value)
{
	$update = mysql_query('UPDATE '.DB_PERFIX.'menu_templates SET cont_type='.$value.' WHERE id='.$id.' LIMIT 1;');
	return $update;
}
function setMenuTemplate($id, $value)
{
	$update = mysql_query('UPDATE '.DB_PERFIX.'menu_templates SET template_id='.$value.' WHERE id='.$id.' LIMIT 1;');
	return $update;
}

function deleteMenu($id)
{
	$result = mysql_query('DELETE FROM '.DB_PERFIX.'menu_templates WHERE parent_id='.$id.';');
	$result = mysql_query('DELETE FROM '.DB_PERFIX.'menu_templates WHERE id='.$id.';');
	return $result;
}
function deleteMenuItem($id)
{
	$result = mysql_query('DELETE FROM '.DB_PERFIX.'menu_templates WHERE parent_id='.$id.';');
	$result = mysql_query('DELETE FROM '.DB_PERFIX.'menu_templates WHERE id='.$id.' AND parent_id>0;');
	return $result;
}
function addMenuItem($id, $name)
{
	$result = mysql_query('SELECT menu_order FROM '.DB_PERFIX.'menu_templates WHERE parent_id='.$id.' ORDER BY menu_order DESC LIMIT 1;');
	while($row = mysql_fetch_array($result))
	{ $neworder = $row['menu_order']; }
	$neworder++;
	
	$result = mysql_query('INSERT INTO '.DB_PERFIX.'menu_templates (`parent_id`, `name`, `menu_order`) VALUES ('.$id.', "'.$name.'", '.$neworder.');');
	return $result;
}
function moveUpMenuItem($id)
{
	$select = mysql_query('SELECT menu_order, parent_id FROM '.DB_PERFIX.'menu_templates WHERE id='.$id.';');
	while($row = mysql_fetch_array($select))
	{ 
		$up_order = $row['menu_order']; 
		$parent = $row['parent_id']; 
	}
	
	$result = mysql_query('SELECT id, menu_order FROM '.DB_PERFIX.'menu_templates WHERE parent_id='.$parent.' ORDER BY menu_order ASC;');
	while($row = mysql_fetch_array($result))
	{
		$current['id'] = $row['id'];
		$current['menu_order'] = $row['menu_order'];
		$down_order = $current['menu_order'] + 1;
		if($down_order == $up_order)
		{
			$update = mysql_query('UPDATE '.DB_PERFIX.'menu_templates SET menu_order='.$down_order.' WHERE id='.$current['id'].' LIMIT 1;');
			$update = mysql_query('UPDATE '.DB_PERFIX.'menu_templates SET menu_order='.$current['menu_order'].' WHERE id='.$id.' LIMIT 1;');
			return;
		}
	}
}
function moveDownMenuItem($id)
{
	$select = mysql_query('SELECT menu_order, parent_id FROM '.DB_PERFIX.'menu_templates WHERE id='.$id.';');
	while($row = mysql_fetch_array($select))
	{ 
		$up_order = $row['menu_order']; 
		$parent = $row['parent_id']; 
	}
	
	$result = mysql_query('SELECT id, menu_order FROM '.DB_PERFIX.'menu_templates WHERE parent_id='.$parent.' ORDER BY menu_order ASC;');
	while($row = mysql_fetch_array($result))
	{
		$current['id'] = $row['id'];
		$current['menu_order'] = $row['menu_order'];
		$down_order = $current['menu_order'] - 1;
		if($down_order == $up_order)
		{
			$update = mysql_query('UPDATE '.DB_PERFIX.'menu_templates SET menu_order='.$down_order.' WHERE id='.$current['id'].' LIMIT 1;');
			$update = mysql_query('UPDATE '.DB_PERFIX.'menu_templates SET menu_order='.$current['menu_order'].' WHERE id='.$id.' LIMIT 1;');
			return;
		}
	}
}

function deleteTemplateRow($id)
{
	$result = mysql_query('DELETE FROM '.DB_PERFIX.'template_cells WHERE row_id='.$id.';');
	$result = mysql_query('DELETE FROM '.DB_PERFIX.'menu_templates WHERE id='.$id.';');
	return $result;
}

function addTemplateCell($row_id)
{	
	$result = mysql_query('INSERT INTO '.DB_PERFIX.'template_cells (`row_id`, `gid`, `active`) VALUES ('.$row_id.', 0, 1);');
	return $result;
}
function deleteTemplateCell($cell_id)
{	
	$result = mysql_query('DELETE FROM '.DB_PERFIX.'template_cells WHERE id='.$cell_id.' LIMIT 1;');
	return $result;
}
function setCellType($cell_id, $cell_type)
{
	$update = mysql_query('UPDATE '.DB_PERFIX.'template_cells SET type='.$cell_type.' WHERE id='.$cell_id.' LIMIT 1;');
	return $update;
}
function setCellContType($cell_id, $content_cat)
{
	$update = mysql_query('UPDATE '.DB_PERFIX.'template_cells SET content_cat='.$content_cat.' WHERE id='.$cell_id.' LIMIT 1;');
	return $update;
}
function setCellContID($cell_id, $content_id)
{
	$update = mysql_query('UPDATE '.DB_PERFIX.'template_cells SET content_id='.$content_id.' WHERE id='.$cell_id.' LIMIT 1;');
	return $update;
}
?>